home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Atari Compendium
/
The Atari Compendium (Toad Computers) (1994).iso
/
files
/
umich
/
utils
/
sort.arc
/
sortmain.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-03-30
|
15KB
|
368 lines
/******************************************************************************
* *
* sortmain.c version 1.0 of 22 Januari 1989 (C) L.J.M. de Wit 1989 *
* *
* This software may be used and distributed freely if not used commercially *
* and the originator (me) is mentioned in the source (just leave this 9 line *
* header intact). *
* *
******************************************************************************
*
* sortmain.c: main module for sort
*/
#include <stdio.h>
#include <ctype.h>
#include "sortmain.h"
#include "sortcomp.h"
#include "sortfile.h"
typedef struct key { /* Sort key structure */
int (*cmp)(); /* Comparision routine */
int bfld; /* Begin field no. */
int bchar; /* Begin char no. */
int echar; /* End char no. */
int flds; /* # of fields (efld-bfld) */
int bis0; /* bfld == bchar == 0 */
int eis0; /* efld == echar == 0 */
struct key *next; /* Next sort key ptr. */
} key;
int options = 0; /* Global sort type flag */
long _mneed = 40000; /* Memory occupied by prog */
static char sepchar = ' '; /* Field separator char */
static key firstkey; /* First key used */
static int compar(); /* Standard compare routine */
static int compb0(), compe0(), compbe(); /* Special cases of compar()*/
static int bdfinr(); /* Get 'bdfinr' options */
static char *(*fieldfunc)();
static char *getsepfld(), *getspafld(); /* Search field + offset */
static int (*getcmp())();
extern void allsort();
main(argc,argv)
int argc;
char **argv;
{
int i, qualif;
char *s;
char *outfile = (char *)0; /* Result file (stdout) */
key *keyp, *keyn;
int (*compfunc)(); /* Routine used to compare */
int nkeys = 0; /* No. of keys to be used */
settemp("\\tmp"); /* Set default temp dir */
initlookup(); /* Initialize lookup arrays */
for (i = 1; i < argc && (*argv[i] == '-' ||
*argv[i] == '+'); i++) { /* Handle commandline flags */
s = argv[i];
if (*s++ == '-') {
if (*s == '\0') { /* '-' denotes std input */
break;
}
for ( ; *s != '\0'; ) {
if (qualif = bdfinr(*s)) { /* *s in (b,d,f,i,n,r) */
options |= qualif; /* Add it to options */
s++;
} else {
switch (*s++) {
case 't': /* Field separator char */
sepchar = *s++;
break;
case 'c': /* Don't sort, only check */
options |= CHECKONLY;
break;
case 'm': /* Merge the sorted input */
options |= MERGEONLY;
break;
case 'o': /* Output file */
options |= OUTFILE;
s += strlen(s);
outfile = argv[++i];
break;
case 'T': /* Directory for temp files */
options |= SEPARATOR;
s += strlen(s);
settemp(argv[++i]);
break;
case 'u': /* Remove duplicate lines */
options |= UNIQUE;
break;
default: /* Invalid option */
*s = '\0'; /* Null terminate */
error("invalid option %s\n",--s);
break;
}
}
}
} else { /* '+' sort key flag */
int keyflags = 0, efld = 0;
if (nkeys++ == 0) {
keyp = &firstkey;
} else {
keyp = (key *)malloc(sizeof(key));/* Create the (next) key */
if (keyp == (key *)0) {
error("memory allocation failed\n",(char *)0);
}
for (keyn = &firstkey; keyn->next != (key *)0;
keyn = keyn->next) ;
keyn->next = keyp;
}
keyp->next = (key *)0;
keyp->bfld = atoi(s); /* Get key's start field no.*/
for ( ; isdigit(*s); s++) ;
if (*s == '.') {
keyp->bchar = atoi(++s); /* Get char offset for field*/
for ( ; isdigit(*s); s++) ;
} else {
keyp->bchar = 0; /* 0 is default */
}
for ( ; *s != '\0'; s++) { /* 'bdfinr' flags for key */
if (qualif = bdfinr(*s)) {
keyflags |= qualif; /* Add flag */
} else {
s[1] = '\0';
error("invalid option %s\n",s);
}
}
if (i + 1 < argc && argv[i+1][0] == '-' /* End field specified */
&& (argv[i+1][1] == '.' || isdigit(argv[i+1][1]))) {
s = argv[++i] + 1;
efld = atoi(s); /* Get key's end field no. */
for ( ; isdigit(*s); s++) ;
if (*s == '.') {
keyp->echar = atoi(++s); /* Get char offset for field*/
for ( ; isdigit(*s); s++) ;
} else {
keyp->echar = 0; /* 0 is default */
}
for ( ; *s != '\0'; s++) { /* 'bdfinr' flags for key */
if (qualif = bdfinr(*s)) {
keyflags |= qualif; /* add flag */
} else {
s[1] = '\0';
error("invalid option %s\n",s);
}
}
} else {
efld = keyp->echar = 0;
}
if (keyflags == 0) { /* If no field flags */
keyflags = options; /* Take global ones */
}
keyp->flds = efld - keyp->bfld;
keyp->bis0 = keyp->bfld == 0 && keyp->bchar == 0;
keyp->eis0 = efld == 0 && keyp->echar == 0;
keyp->cmp = getcmp(keyflags,keyp->eis0);
}
}
bestio(2048); /* Possibly initiate bestio */
if (nkeys == 0) { /* No key specified */
nkeys++;
firstkey.next = (key *)0;
firstkey.cmp = getcmp(options,1);
firstkey.bfld = 0;
firstkey.bchar = 0;
firstkey.echar = 0;
firstkey.flds = 0;
firstkey.bis0 = 1;
firstkey.eis0 = 1;
}
fieldfunc = (options & SEPARATOR) ? getsepfld : getspafld;
if (nkeys == 1) { /* One key only */
if (firstkey.bis0) {
if (firstkey.eis0) {